<template>
	<!-- 测试 -->
	<view>
		<!-- 文字转语音 -->
		<text>{{contents}}</text>
		<text>{{token}}</text>
		<button type="default" @click="getBaiduToken">getBaiduToken</button>
		<button type="default" @click="play2">play</button>

		<!-- 语音识别 -->
		<view class="content">
			<button @click="startLuyin">按住开始说话</button>
			<button @touchstart="startRecord" @touchend="endRecord">按住开始说话2</button>
			<view>识别的结果 : {{ searchText }}</view>
		</view>
		<!-- 紧急呼叫 -->
		<view class="phone">
			<button type="default" @click="callNoLock">紧急呼叫（不拉权限）</button>
			<button type="default" @click="call">紧急呼叫</button>

		</view>

	</view>
</template>

<script>
	//录音
	const recorderManager = uni.getRecorderManager();
	//播放录音
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = true;


	export default {
		data() {
			return {
				contents: "你好世界",
				searchText: '',
				token: '',
				speechEngine: 'baidu',
			}
		},
		onLoad() {
			// this.start();

		},
		methods: {
			//获取百度语音播报token
			getBaiduToken() {
				uni.request({
					method: 'Post',
					url: "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=YAFs1aZjyRzorykBeqz6uE8X&client_secret=hGjfyqnXyhuXvh8Ocs9EBdXeKavsZ39r&",
					header: {
						'Content-Type': 'application/json',
						'Accept': 'application/json',
					},
					data: {

					},
					success: (data) => {
						console.log(data);
						this.token = data.data.access_token
					}
				})
			},
			play() {
				uni.request({
					method: 'POST',
					url: "http://tsn.baidu.com/text2audio",
					headers: {
						// tok: this.token
						'Content-Type': 'application/x-www-form-urlencoded',
						'Accept': '*/*'
					},
					data: {
						tex: '%E2%80%9C%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C%E2%80%9D', //转换格式
						tok: this.token,
						cuid: '10.100.80.51',
						ctp: 1,
						lan: 'zh',
					},
					success: (data) => {
						console.log(data)

					}
				})

			},

			play2() {
				const innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = 'https://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=abcdxxx&tok=' + this.token + '&tex=' +
					encodeURIComponent(this.contents);
				innerAudioContext.onPlay(() => {
					console.log('开始播放');
				});
			},

			startLuyin() {
				console.log('语音输入')
				let _this = this;
				let options = {};
				options.engine = _this.speechEngine
				options.punctuation = false; // 是否需要标点符号 
				options.timeout = 10 * 1000; //超时时间
				// console.log('1111')

				plus.speech.startRecognize(options, function(s) {
						console.log('开始识别')
						console.log(s) //识别的结果
						_this.searchText = s
						_this.searchText = 'hello world'
						plus.speech.stopRecognize(); // 关闭
					},

					function(e) {
						console.log('语音识别失败：' + JSON.stringify(e));
					});

			},
			startRecord() {
				console.log('开始录音');
				// 转文字

				recorderManager.start({
					duration: 180000
				});
				this.startLuyin()
			},
			endRecord() {},

			callNoLock() {
				// 导入Activity、Intent类  
				var Intent = plus.android.importClass("android.content.Intent");
				var Uri = plus.android.importClass("android.net.Uri");
				// 获取主Activity对象的实例  
				var main = plus.android.runtimeMainActivity();
				// 创建Intent  
				var uri = Uri.parse("tel:10086"); // 这里可修改电话号码  
				var call = new Intent("android.intent.action.CALL", uri);
				// 调用startActivity方法拨打电话  
				main.startActivity(call);

			},
			call() {
				uni.makePhoneCall({
					phoneNumber: '18575842930', //电话
					success: (data) => {
						console.log('sucess', data)
					},
					fail: (data) => {
						console.log('fail', data)
					}
				});

			},

		}
	}
</script>

<style>
	/* 	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
		height: 200rpx;
		width: 200rpx;
		background-color: #007AFF;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	} */
</style>
